package com.sinooceangroup.voiceassistant;

import android.content.Context;
import android.content.res.AssetManager;

import com.iflytek.aiui.AIUIAgent;
import com.iflytek.aiui.AIUIConstant;
import com.iflytek.aiui.AIUIListener;
import com.iflytek.aiui.AIUIMessage;
import com.sinooceanland.commonutils.SharedPreferencesUtil;

import java.io.IOException;
import java.io.InputStream;

import static com.sinooceangroup.voiceassistant.Constants.FEMALE_VOICE;
import static com.sinooceangroup.voiceassistant.Constants.MALE_OR_FEMALE_VOICE;

/**
 * AIUI处理
 */
public class AIUIManager {

    private static final String TAG = "AIUIManager";


    public AIUIAgent mAIUIAgent;
    public AIUIListener mAIUIListener;
    private Context context;
    private int mAIUIState = AIUIConstant.STATE_IDLE;

    private boolean mAudioRecording = false;
    private static AIUIManager aiuiManager;



    public static AIUIManager getInstance() {
        if (aiuiManager == null)
            aiuiManager = new AIUIManager();
        return aiuiManager;
    }

    public void initAIUI(Context context, AIUIListener mAIUIListener) {
        this.context = context;
        this.mAIUIListener = mAIUIListener;
        mAIUIAgent = AIUIAgent.createAgent(context, getAIUIParams(), mAIUIListener);
    }


    private String getAIUIParams() {
        String params = "";
        AssetManager assetManager = context.getResources().getAssets();
        try {
            InputStream ins = assetManager.open("cfg/aiui_phone.cfg");
            byte[] buffer = new byte[ins.available()];

            ins.read(buffer);
            ins.close();

            params = new String(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return params;
    }




    public void setAIUIState(int mAIUIState) {
        this.mAIUIState = mAIUIState;
    }

    public AIUIAgent getAIUIAgent() {
        return mAIUIAgent;
    }

    public void startTTS(String text) {
        String tag = "@" + System.currentTimeMillis();

        StringBuffer params = new StringBuffer();  //构建合成参数
        params.append("vcn="+ SharedPreferencesUtil.getString(context, MALE_OR_FEMALE_VOICE, FEMALE_VOICE));  //合成发音人
        params.append(",speed="+SharedPreferencesUtil.getString(context, Constants.SPEAKING_RATE, "50"));  //合成速度
        params.append(",pitch=50");  //合成音调
        params.append(",volume=50");  //合成音量
        params.append(",ent=xtts");  //合成音量
        params.append(",tag=" + tag);  //合成tag，方便追踪合成结束，暂未实现
        AIUIMessage startTts = new AIUIMessage(AIUIConstant.CMD_TTS, AIUIConstant.START, 0,
                params.toString(), text.getBytes());
        sendMessage(startTts);
    }

    public void pauseTTS() {
        sendMessage(new AIUIMessage(AIUIConstant.CMD_TTS, AIUIConstant.PAUSE, 0, null, null));
    }

    /**
     * 停止合成播放
     */
    public void stopTTS() {
        sendMessage(new AIUIMessage(AIUIConstant.CMD_TTS, AIUIConstant.CANCEL, 0, "", null));
    }



    public boolean isAudioRecording() {
        return mAudioRecording;
    }

    public void setAudioRecording(boolean mAudioRecording) {
        this.mAudioRecording = mAudioRecording;
    }

    /**
     * 开始录音
     */
    public void startRecordAudio(String tag) {
        if (!mAudioRecording) {
            String params = "data_type=audio,sample_rate=16000,dwa=wpgs,tag="+tag;
            sendMessage(new AIUIMessage(AIUIConstant.CMD_START_RECORD, 0, 0, params, null));
            mAudioRecording = true;
        }
    }

    /**
     * 停止录音
     */
    public void stopRecordAudio() {
        if (mAudioRecording) {
            sendMessage(new AIUIMessage(AIUIConstant.CMD_STOP_RECORD, 0, 0, "data_type=audio,sample_rate=16000", null));
            mAudioRecording = false;
        }
        sendMessage(null);
    }


    /**
     * 发送AIUI消息
     *
     * @param message
     */
    public void sendMessage(AIUIMessage message) {
        if (mAIUIAgent != null) {
            //确保AIUI处于唤醒状态
            if (mAIUIState != AIUIConstant.STATE_WORKING) {
                mAIUIAgent.sendMessage(new AIUIMessage(AIUIConstant.CMD_WAKEUP, 0, 0, "", null));
            }

            mAIUIAgent.sendMessage(message);
        }
    }


    public void wakeupAIUI() {
        if (AIUIConstant.STATE_WORKING != mAIUIState) {
            AIUIMessage wakeupMsg = new AIUIMessage(AIUIConstant.CMD_WAKEUP, 0, 0, "", null);
            sendMessage(wakeupMsg);
        }
    }

}
